﻿namespace HynuIOTA.Web.Areas.Admin.Controllers
{
    using HynuIOTA.Common.Attribute;
    using HynuIOTA.Entity;
    using HynuIOTA.Web.Filters;
    using HynuIOTA.Web.Helpers;
    using HynuIOTA.Web.Models;
    using System;
    using System.Data.Entity;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Web.Mvc;
    using static HynuIOTA.Common.StringConst;

    public class NoticeController : BaseController
    {
        /// <summary>
        /// GET: Admin/Notice/NoticeList
        /// 通知列表页面
        /// </summary>
        /// <returns></returns>
        public ActionResult NoticeList()
        {
            return View();
        }

        /// <summary>
        /// GET: Admin/Notice/AddNotice
        /// 添加通知
        /// </summary>
        /// <returns></returns>
        public ActionResult AddNotice()
        {
            return View();
        }

        /// <summary>
        /// GET: Admin/Notice/EditNotice
        /// 编辑通知
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<ActionResult> EditNotice(int? Id)
        {
            if (Id == null)
            {
                return OpContext.JsMsg(MODEL_VALIDATE_ERROR);
            }

            var notice = await OpContext.Service.Notice.Where(n => n.Id == Id.Value).FirstOrDefaultAsync();
            if (notice == null)
            {
                return OpContext.JsMsg(DATA_NOEXIST_ERROR);
            }

            ViewBag.notice = notice;
            return View();
        }

        /// <summary>
        /// ANY: Admin/Notice/GetNoticePage
        /// 获取通知分页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        [AjaxRequest]
        public async Task<ActionResult> GetNoticePage(int page = 1, int rows = int.MaxValue)
        {
            var total = await OpContext.Service.Notice.Where(n => true).CountAsync();
            var pageList = await OpContext.Service.Notice.Where(n => true).OrderByDescending(n => n.CreateTime).Skip((page - 1) * rows).Take(rows)
                .Select(n => new { n.Id, n.Title, n.User.UserName, n.CreateTime }).ToListAsync();

            return OpContext.JsonMsgOK(data: new { total, rows = pageList });
        }

        /// <summary>
        /// POST: Admin/Notice/PostNotice
        /// 添加通知
        /// </summary>
        /// <param name="noticeViewModel"></param>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public async Task<ActionResult> PostNotice(PostNoticeViewModel noticeViewModel)
        {
            if (ModelState.IsValid == false)
            {
                return OpContext.JsonMsgFail(MODEL_VALIDATE_ERROR);
            }

            var notice = new Notice()
            {
                Title = noticeViewModel.Title,
                Content = noticeViewModel.Content,
                CreateTime = DateTime.Now,
                UserId = OpContext.UserEntity.Id
            };
            OpContext.Service.Notice.Add(notice);
            var result = await OpContext.SaveChangesAsync();

            if (result != null)
            {
                return result;
            }
            OpContext.RefreshCacheDependency(CacheDependencyKeys.Notice);
            return OpContext.JsonMsgOK(DATA_ADD_SUCCESS);
        }

        /// <summary>
        /// DELETE: Admin/Notice/DeleteNotice
        /// 删除通知
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        [AjaxRequest]
        public async Task<ActionResult> DeleteNotice(int? Id)
        {
            if (Id == null)
            {
                return OpContext.JsonMsgFail(MODEL_VALIDATE_ERROR);
            }

            var notice = await OpContext.Service.Notice.Where(n => n.Id == Id.Value).FirstOrDefaultAsync();
            if (notice == null)
            {
                return OpContext.JsonMsgFail(DATA_NOEXIST_ERROR);
            }

            OpContext.Service.Notice.Delete(notice);
            var result = await OpContext.SaveChangesAsync();

            if (result != null)
            {
                return result;
            }
            OpContext.RefreshCacheDependency(CacheDependencyKeys.Notice);
            return OpContext.JsonMsgOK(DATA_DELECT_SUCCESS);
        }

        /// <summary>
        /// PUT: Admin/Notice/PutNotice
        /// 修改通知
        /// </summary>
        /// <param name="noticeViewModel"></param>
        /// <returns></returns>
        [HttpPut]
        [AjaxRequest]
        public async Task<ActionResult> PutNotice(PutNoticeViewModel noticeViewModel)
        {
            if (ModelState.IsValid == false)
            {
                return OpContext.JsonMsgFail(MODEL_VALIDATE_ERROR);
            }

            var notice = await OpContext.Service.Notice.Where(n => n.Id == noticeViewModel.Id).FirstOrDefaultAsync();
            if (notice == null)
            {
                return OpContext.JsonMsgFail(DATA_NOEXIST_ERROR);
            }
            
            Tools.SetValues(notice,noticeViewModel,ModelState.Keys.ToArray());
            var result = await OpContext.SaveChangesAsync();

            if (result != null)
            {
                return result;
            }
            OpContext.RefreshCacheDependency(CacheDependencyKeys.Notice);
            return OpContext.JsonMsgOK(DATA_MODIFY_SUCCESS);
        }
    }
}